package cn.sky.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.session.web.http.HttpSessionIdResolver;

@Configuration
@EnableRedisHttpSession
public class SessionConfig {

   /* @Bean
    public LettuceConnectionFactory connectionFactory() {
        // Redis连接配置
        RedisStandaloneConfiguration redisConfig=new RedisStandaloneConfiguration();
        redisConfig.setHostName("192.168.0.162");
        redisConfig.setPort(6379);
        redisConfig.setDatabase(0);
        redisConfig.setPassword("123456");
        
        return new LettuceConnectionFactory(redisConfig);
    }*/
    
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer=new DefaultCookieSerializer();
        serializer.setCookieName("SESSION"); // 使用统一的Cookie名
        serializer.setCookiePath("/");
        serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$"); // 支持子域
        serializer.setUseSecureCookie(true);
        serializer.setUseHttpOnlyCookie(true);
        
        return serializer;
    }
    
    @Bean
    public HttpSessionIdResolver httpSessionIdResolver() {
        return new CookieHttpSessionIdResolver();
    }
}